/*
 * @(#)ModelUtils.java
 * Date 2013-08-24
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.utils;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import us.hornerscorners.lamppost.config.ModelConfig;

import us.hornerscorners.lamppost.model.common.CodedElement;

import us.hornerscorners.lamppost.model.patient.Patient;
import us.hornerscorners.lamppost.model.patient.PersonName;
import us.hornerscorners.lamppost.model.patient.PersonalRelationship;


/**
 *
 * @author jim
 */
public class ModelUtils {

    /**
     * Method description
     *
     *
     * @param modelConfig
     * @param patient
     *
     * @return
     */
    public static String parseMaidenRelationship(ModelConfig modelConfig,
            Patient patient) {

        List<PersonName> motherNames = null;

        String oid = modelConfig.getRelationshipCodeSystem();
        String code = modelConfig.getRelationshipMothersMaidenNameCode();

        for (PersonalRelationship relationship :
                patient.getPersonalRelationships()) {

            CodedElement ce = relationship.getRelationshipCode();

            if ((ce != null) && StringUtils.equals(oid, ce.getCodeSystem())) {

                if (StringUtils.equalsIgnoreCase(code, ce.getCode())) {

                    motherNames = relationship.getPersonNames();

                    break;
                }
            }
        }

        String result = null;

        if (motherNames != null) {

            PersonName motherName = parsePreferredName(modelConfig,
                                        motherNames);

            if (motherName != null) {

                result = motherName.getLastName();
            }
        }

        return result;
    }

    /**
     * Method description
     *
     *
     * @param modelConfig
     * @param personNames
     *
     * @return
     */
    public static PersonName parsePreferredName(ModelConfig modelConfig,
            List<PersonName> personNames) {

        PersonName result = null;

        String prefCode = modelConfig.getNameUsePreferredCode();

        for (PersonName name : personNames) {

            // find a blank one or preferred one
            CodedElement elem = name.getPurpose();

            if (elem == null) {

                // find first blank one but continue searching
                if (result == null) {

                    result = name;
                }

            } else if (StringUtils.equals(prefCode, elem.getCode())) {

                result = name;

                break;
            }
        }

        // last ditch effort
        if ((result == null) && (personNames.isEmpty() == false)) {

            // just grab first one
            result = personNames.get(0);
        }

        return result;
    }

    /**
     * Method description
     *
     *
     * @param modelConfig
     * @param patient
     *
     * @return
     */
    public static PersonName parsePreferredName(ModelConfig modelConfig,
            Patient patient) {

        PersonName result = null;

        if (patient != null) {
            result = parsePreferredName(modelConfig, patient.getPersonNames());
        }

        return result;
    }
}
